<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../../header.jsp"%>

<div class="left ">
<div class="border bg">
<div class="page-header">
<h2>项目管理</h2>
<div class="cms_toolbar">
	<a href="list.do" class="btn"><i class="icon-list"></i>返回</a>
</div>
</div>

<div>
<form id="detailForm" class="form-horizontal">
<input type="hidden" id="id" name="id" value="${param.id}" />
<fieldset>
	<div class="control-group">
		<label for="name"  class="control-label">名称：</label>
		<div class="controls">
		<input type="text" id="name" name="name" class="required" maxlength="50" /> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="year" class="control-label">年度：</label>
		<div class="controls">
		<input type="text" id="year" name="year" class="required" maxlength="4" /> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="organId" class="control-label">分公司：</label>
		<div class="controls">
		<select name="organId" id="organId">
		<c:forEach var="organ" items="${organs}">
			<option value="${organ.id}">${organ.name}</option>
		</c:forEach>
		</select>
		<span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="areaName" class="control-label">地区：</label>
		<div class="controls">
		<input type="hidden" id="areaId" name="areaId">
		<input type="text" id="areaName" name="areaName" readonly="readonly" />
		<a href="javascript:;" class="btn" onclick="selectArea()">选择</a>
		<span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="customerId" class="control-label">客户：</label>
		<div class="controls">
		<select name="customerId" id="customerId">
		<c:forEach var="customer" items="${customers}">
			<option value="${customer.id}">${customer.name}</option>
		</c:forEach>
		</select>
		</div>
	</div>
	<div class="control-group">
		<label for="specialId" class="control-label">专业：</label>
		<div class="controls">
		<select name="specialId" id="specialId">
		<c:forEach var="special" items="${specials}">
			<option value="${special.id}">${special.name}</option>
		</c:forEach>
		</select>
		</div>
	</div>
	<div class="control-group">
		<label for="close" class="control-label">状态：</label>
		<div class="controls">
		<select name="close" id="close">
			<option value="0">正常</option>
			<option value="1">关闭</option>
		</select>
		</div>
	</div>
	<div class="control-group hide">
		<label for="description" class="control-label">描述：</label>
		<div class="controls">
		<textarea id="description" name="description"></textarea>
		</div>
	</div>
	<div class="form-actions">
		<a href="javascript:void(0);" class="btn btn-primary" onclick="doSave()"><i class="icon-ok"></i>保存</a>
	</div>
</fieldset>
</form>
</div>

</div>
</div>

<script type="text/javascript">
function selectArea() {
	$.openDialog("#dialog",
			{height:350,
			width:300,
			minHeight:350,
			minWidth:300,
			loadPage:ctx + "/biz/dict/area/tree.jsp",
			title:"选择地区"},
			function(data) {
				$("#areaId").val(data.data("id"));
				$("#areaName").val(data.data("name"));
			});
}

function doSave() {
	if (!detailValidator.form()) {
		return;
	}
	
	block("#detailForm");
	$.ajax({
		type: "POST",
		url: "save.json",
		data: $("#detailForm").serialize(),
		dataType: "json",
		success: saveSucceedCallback,
		error:function(message){
			unblock("#detailForm");
			$.alert("出错了，错误信息为：\n"+message);
		}
	});
}

function saveSucceedCallback(data) {
	unblock("#detailForm");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}

	$.info("保存成功！");
	if ($("#id").val() == "") {
		var url = "edit.do?id=" + data.data;
		location.href = url;
	}
}

function loadDetail() {
	block("#detailForm");
	$.ajax({
		type: "GET",
		url: "detail.json",
		data: {id : $("#id").val()},
		dataType: "json",
		success: function(json) {
			unblock("#detailForm");
			$("#name").val(json.data.name);
			$("#year").val(json.data.year);
			$("#organId").val(json.data.organId);
			$("#areaId").val(json.data.areaId);
			$("#areaName").val(json.data.areaName);
			$("#customerId").val(json.data.customerId);
			$("#specialId").val(json.data.specialId);
			$("#close").val(json.data.close ? 1 : 0);
		},
		error:function(message){
			unblock("#detailForm");
			$.alert("出错了，错误信息为：\n"+message);
		}
	});
}

var detailValidator;
$(document).ready(function() {
	detailValidator = $("#detailForm").validate({
		rules: {
			name:{required:true, maxlength:50 },
			year:{required:true, digits:true, max:2050, min:2010},
			areaName:{required:true},
			organId:{required:true},
			customerId:{required:true},
			specialId:{required:true}
		}
	});
	
	if($("#id").val() != ""){
		loadDetail();
	}
});


</script>


<%@ include file="../../../footer.jsp"%>